// package cn.com.nes.mybatis.common.sqlInjector;
//
// import cn.com.nes.mybatis.MyMapper;
// import cn.com.nes.mybatis.common.annotation.BusinessKey;
// import cn.com.nes.mybatis.common.annotation.LogicDelete;
// import cn.hutool.core.util.StrUtil;
// import com.baomidou.mybatisplus.annotation.TableField;
// import com.baomidou.mybatisplus.core.injector.AbstractMethod;
// import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
// import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
// import com.baomidou.mybatisplus.core.injector.SqlRunnerInjector;
// import com.baomidou.mybatisplus.core.metadata.TableInfo;
// import org.apache.ibatis.builder.MapperBuilderAssistant;
// import org.apache.ibatis.mapping.SqlSource;
// import org.apache.ibatis.session.Configuration;
//
// import java.lang.reflect.Field;
// import java.util.Collections;
// import java.util.List;
//
// public class LogicDeleteByBusinessKey extends DefaultSqlInjector {
//     @Override
//     public void inject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass,
//                        Class<?> modelClass, TableInfo table) {
//         // 逻辑删除
//         logicDeleteByBusinessKey(mapperClass, modelClass, table);
//         // 逻辑删除并更新版本号
//         logicDeleteByBusinessKeyAndUpdateVersion(mapperClass, modelClass, table);
//     }
//
//     private void logicDeleteByBusinessKeyAndUpdateVersion(Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
//         // 必须继承了MyMapper
//         if (MyMapper.class.isAssignableFrom(mapperClass)) {
//             // 获取带有BusinessKey注解的字段
//             Field[] fields = modelClass.getDeclaredFields();
//             String businessKeyColumn = null;
//             for (Field field : fields) {
//                 if (field.isAnnotationPresent(BusinessKey.class)) {
//                     if (field.isAnnotationPresent(TableField.class)) {
//                         businessKeyColumn = field.getAnnotation(TableField.class).value();
//                     } else {
//                         // 驼峰
//                         businessKeyColumn = StrUtil.toUnderlineCase(field.getName());
//                     }
//                     break;
//                 }
//             }
//             // 获取带有LogicDelete注解的字段
//             String deletedColumn = null;
//             for (Field field : fields) {
//                 if (field.isAnnotationPresent(LogicDelete.class)) {
//                     if (field.isAnnotationPresent(TableField.class)) {
//                         deletedColumn = field.getAnnotation(TableField.class).value();
//                     } else {
//                         deletedColumn = StrUtil.toUnderlineCase(field.getName());
//                     }
//                     break;
//                 }
//             }
//             boolean enableLogicDelete = businessKeyColumn != null && deletedColumn != null;
//             if (enableLogicDelete) {
//                 /* 执行 SQL ，动态 SQL 参考类 SqlMethod */
//                 String sql = "UPDATE " + table.getTableName() +
//                         " SET " + deletedColumn + " = 1," +
//                         " version = #{version}" +
//                         " WHERE " + businessKeyColumn + " = #{businessKey}";
//                 /* mapper 接口方法名一致 */
//                 String method = "logicDeleteByBusinessKeyAndUpdateVersion";
//
//                 SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
//                 this.addUpdateMappedStatement(mapperClass, modelClass, method, sqlSource);
//             }
//         }
//     }
//
//     public void logicDeleteByBusinessKey(Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
//         // 必须继承了MyMapper
//         if (MyMapper.class.isAssignableFrom(mapperClass)) {
//             // 获取带有BusinessKey注解的字段
//             Field[] fields = modelClass.getDeclaredFields();
//             String businessKeyColumn = null;
//             for (Field field : fields) {
//                 if (field.isAnnotationPresent(BusinessKey.class)) {
//                     if (field.isAnnotationPresent(TableField.class)) {
//                         businessKeyColumn = field.getAnnotation(TableField.class).value();
//                     } else {
//                         // 驼峰
//                         businessKeyColumn = StrUtil.toUnderlineCase(field.getName());
//                     }
//                     break;
//                 }
//             }
//             // 获取带有LogicDelete注解的字段
//             String deletedColumn = null;
//             for (Field field : fields) {
//                 if (field.isAnnotationPresent(LogicDelete.class)) {
//                     if (field.isAnnotationPresent(TableField.class)) {
//                         deletedColumn = field.getAnnotation(TableField.class).value();
//                     } else {
//                         deletedColumn = StrUtil.toUnderlineCase(field.getName());
//                     }
//                     break;
//                 }
//             }
//             boolean enableLogicDelete = businessKeyColumn != null && deletedColumn != null;
//             if (enableLogicDelete) {
//                 /* 执行 SQL ，动态 SQL 参考类 SqlMethod */
//                 String sql = "UPDATE " + table.getTableName() +
//                         " SET " + deletedColumn + " = 1" +
//                         " WHERE " + businessKeyColumn + " = #{businessKey}";
//                 /* mapper 接口方法名一致 */
//                 String method = "logicDeleteByBusinessKey";
//
//                 SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
//                 this.addUpdateMappedStatement(mapperClass, modelClass, method, sqlSource);
//             }
//         }
//     }
//
//     @Override
//     public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
//         return Collections.emptyList();
//     }
//
// }
